package com.aaa.vegetablemarketexpress.controller;

import com.aaa.vegetablemarketexpress.entity.Contact;
import com.aaa.vegetablemarketexpress.entity.User;
import com.aaa.vegetablemarketexpress.service.ContactService;
import com.aaa.vegetablemarketexpress.util.Page;
import com.aaa.vegetablemarketexpress.util.Result;
import com.aaa.vegetablemarketexpress.util.ResultUtil;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @FileName: ContactController
 * @Description:
 * @Author: 张钰哲
 * @CreateTime: 2025/9/22 20:01
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("contact")
public class ContactController {
    @Resource
    private ContactService contactService;

    /**
     * 用户提交咨询
     * @param contact
     * @return
     */
    @PostMapping("/submit")
    public Result submitContact(@RequestBody Contact contact) {
        try {
            // 基本参数校验
            if (contact.getName() == null || contact.getName().trim().isEmpty()
                    || contact.getContent() == null || contact.getContent().trim().isEmpty()) {
                return ResultUtil.failed("姓名和咨询内容不能为空");
            }

            int rows = contactService.submitMessage(contact);
            return rows > 0 ? ResultUtil.success("咨询提交成功") : ResultUtil.failed("咨询提交失败");
        } catch (Exception e) {
            return ResultUtil.failed("提交失败：" + e.getMessage());
        }
    }

    /**
     * 分页查询咨询列表
     * @param page
     * @return
     */
    @PostMapping("/page")
    public Result getContactPage(@RequestBody Page<Contact> page) {
        try {
            PageInfo<Contact> pageInfo = contactService.findByPage(page);
            return ResultUtil.success(pageInfo);
        } catch (IllegalArgumentException e) {
            return ResultUtil.failed(e.getMessage());
        } catch (Exception e) {
            return ResultUtil.failed("查询失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID查询咨询详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getContactDetail(@PathVariable Long id) {
        try {
            Contact contact = contactService.selectById(id);
            if (contact == null) {
                return ResultUtil.failed("咨询信息不存在");
            }
            return ResultUtil.success(contact);
        } catch (IllegalArgumentException e) {
            return ResultUtil.failed(e.getMessage());
        } catch (Exception e) {
            return ResultUtil.failed("查询失败：" + e.getMessage());
        }
    }

    /**
     * 管理员回复咨询
     * @param id
     * @param replyContent
     * @param session
     * @return
     */
    @PostMapping("/reply/{id}")
    public Result replyToContact(@PathVariable Long id,
                                 @RequestParam String replyContent,
                                 HttpSession session) {
        try {
            // 获取当前登录管理员ID（实际项目中应从认证信息中获取）
            User admin = (User) session.getAttribute("userInfo");
            if (admin == null) {
                return ResultUtil.failed("请先登录");
            }

            int rows = contactService.replyMessage(id, replyContent, admin.getUserId());
            return rows > 0 ? ResultUtil.success("回复成功") : ResultUtil.failed("回复失败");
        } catch (RuntimeException e) {
            return ResultUtil.failed(e.getMessage());
        } catch (Exception e) {
            return ResultUtil.failed("回复失败：" + e.getMessage());
        }
    }
    /**
     * 获取用户历史咨询
     * @param session
     * @return
     */
    @GetMapping("/history")
    public Result<List<Contact>> getHistory(HttpSession session) {
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultUtil.failed("用户未登录");
        }
        List<Contact> historyList = contactService.getByUserId(user.getUserId());
        return ResultUtil.success(historyList);
    }
}
